﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.FrmKP
{
    public partial class FrmSK : Form
    {

        public Vouch.VouchToolBar voudefine;
        public FrmSK()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Fly;
            voudefine = new Vouch.VouchToolBar("salebill", this.groupBox1, this.toolStrip1, "税控发票列表");
            voudefine.Init();
            foreach (ToolStripItem bar in this.toolStrip1.Items)
            {
                if (bar.Text.Length > 0)
                { bar.Click += toolbar_click; }
            }
        }

        #region 工具栏事件
        private void toolbar_click(object sender, EventArgs e)
        {
            if (sender.GetType() != typeof(ToolStripButton)) { return; }
            var bt = (ToolStripButton)sender;
            var text = bt.Text;
            toolbar_click(text);
        }
        public void toolbar_click(string text)
        {
            voudefine._dg.LeftEdit();
            switch (text)
            {
                case "修改":
                    Modify();
                    break;
                case "保存":
                    SaveVouch();
                    break;
                case "放弃":
                    Cancel();
                    break;
                case "重新传输税控":
                    VouchToFPRe();
                    break;
                case "电票开具状态":
                    voudefine.VouchStatus_SK();
                    break;
                case "PDF":
                    voudefine.DownloadPDF_SK(voudefine._id);
                    break;
                case "退出":
                    this.Close();
                    break;
            }
        }
        #endregion 


        #region 重新传输税控
        public void VouchToFPRe()
        {
            try
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/GetOne?token={1}&ID={2}",
        ServerConfig.ServerAddress, ServerConfig.GetToken(), voudefine._id);
                var val = Common.handleHttp.Get(url);
                var result = Common.handleJSON.DeserializeResult(val);
                if (result.result != 1) { MessageBox.Show("出错:" + result.resultdetail); return; }

                #region 检查
                var sk = result.Get<Model.SK_SalebillVouch>("main");
                voudefine._id = sk.ID;
                var l_sks = result.GetList<Model.SK_SalebillVouchs_View>("details");
                string err="";
                #endregion

                err = cSK.SoftSel.proc(sk, l_sks);
                if (err.Length > 0) { MessageBox.Show(err); }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 修改
        public void Modify()
        {
            try
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/Modify?token={1}&id={2}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), voudefine._id);
                var d = new Dictionary<string, object>();
                d.Add("ID", voudefine._id); d.Add("ts", voudefine._ts);
                var s = Common.handleHttp.Post(d, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("出错:" + result.resultdetail); return; }

                #region 显示数据
                var rd = (Dictionary<string, object>)result.GetVal("main");
                voudefine._id = rd["ID"].ObjToInt();
                voudefine._ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("details");
                voudefine.DisplayDataForm( rd);
                voudefine.Displaydata(rds, true);
                voudefine.SetToolBarEnable(false);
                voudefine._bVouchModifyStatus = true;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 保存单据
        public void SaveVouch()
        {
            try
            {
                string err;
                List<Dictionary<string, object>> l_detail; ;
                var main = voudefine.GetVouchData(out err, out l_detail);
                if (err.Length > 0) { MessageBox.Show("数据校验出现错误：" + err); return; }
                string url;
                url = url = string.Format("{0}/api/oms/SKSalebillVouch/UpdateTry?token={1}",
                      ServerConfig.ServerAddress, ServerConfig.GetToken());
                var postdic = new Dictionary<string, object>();
                postdic.Add("main", main);
                postdic.Add("detail", l_detail);
                var poststr = Common.handleJSON.Serialize(postdic);
                var s = Common.handleHttp.Post(poststr, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result == 1001)
                {
                    var dr = MessageBox.Show(result.resultdetail, "警告", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.No)
                    { return; }
                }
                else
                {
                    if (result.result != 1) { MessageBox.Show(result.resultdetail); }
                }

                url = string.Format("{0}/api/oms/SKSalebillVouch/Update?token={1}",
                      ServerConfig.ServerAddress, ServerConfig.GetToken());
                s = Common.handleHttp.Post(poststr, url);
                result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("保存出错:" + result.resultdetail); return; }

                #region 显示数据
                var rd = (Dictionary<string, object>)result.GetVal("main");
                voudefine._id = rd["ID"].ObjToInt();
                voudefine._ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("details");
                voudefine.DisplayDataForm( rd);
                voudefine.Displaydata(rds, true);
                voudefine.SetToolBarEnable(true);
                voudefine._bVouchModifyStatus = false;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }

        }
        #endregion

        #region 放弃
        public void Cancel()
        {
            try
            {
                if (voudefine._id == 0)
                {
                    var customer = new Dictionary<string, object>();
                    var rd = new Dictionary<string, object>();
                    voudefine._id = 0;
                    voudefine._ts = "";
                    var rds = new List<Dictionary<string, object>>();
                    voudefine.DisplayDataForm(customer, rd);
                    voudefine.Displaydata(rds, true);
                    voudefine.SetToolBarEnable(true);
                    voudefine._bVouchModifyStatus = false;
                }
                else { Reflesh(voudefine._id); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 显示数据
        public void Reflesh(int id)
        {
            try
            {
                voudefine._id = id;
                var url = string.Format("{0}/api/oms/SKSalebillVouch/GetOne?token={1}&ID={2}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), voudefine._id);
                var s = Common.handleHttp.Get(url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("出错:" + result.resultdetail); return; }

                #region 显示数据
                var rd = (Dictionary<string, object>)result.GetVal("main");
                voudefine._id = rd["ID"].ObjToInt();
                voudefine._ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("details");
                voudefine.DisplayDataForm( rd);
                voudefine.Displaydata(rds, true);
                voudefine.SetToolBarEnable(true);
                voudefine._bVouchModifyStatus = false;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

    }
}
